package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Resource
    private EmpExprService empExprService;
    @Resource
    private DeptService deptService;
    @Resource
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        // 创建分页模型对象，设置页码和每页数量
        Page<Emp> pages = Page.of(param.getPage(), param.getPageSize());
        // 添加排序条件：根据最后修改时间倒序
        pages.addOrder(OrderItem.desc("entry_date"));

        //添加条件筛选
        LambdaQueryWrapper<Emp> queryWrapper = Wrappers.<Emp>lambdaQuery()
                .like(param.getName()!= null && !param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());

        // 调用 从父类里继承来的page方法完成分页查询
        page(pages,queryWrapper);

        //补齐部门名称
        //获取所有部门
        List<Emp> emps = pages.getRecords();
        //获取所有员工的部门id,去掉重复
        List<Integer> deptIds = emps.stream().map(Emp::getDeptId).distinct().toList();
        //根据deptIds查询所有部门信息
        List<Dept> depts = deptService.listByIds(deptIds);
        //将depts中的id为key，name为value，生成map集合
        Map<Integer, String> collect = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));

        //遍历添加部门名称
        for (Emp emp : emps) {
            //根据id查找部门名称
            emp.setDeptName(collect.get(emp.getDeptId()));
        }


        return new PageResult<>(pages.getTotal(),emps);
    }

    /**
     * 添加员工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        // 调用service层方法保存员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        // 调用service层方法保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {expr.setEmpId(emp.getId());});
        empExprService.saveBatch(exprList);
//        empExprMapper.insert(exprList);
    }
}
